package com.hzhc.controller.sysmanage;

import com.hzhc.entity.SysDict;
import com.hzhc.service.SysDictService;
import com.hzhc.utils.BeanUtils;
import com.hzhc.utils.ResultUtil;
import com.hzhc.utils.YmlUtil;
import com.hzhc.vo.SysDictVo;
import lombok.RequiredArgsConstructor;
import lombok.extern.log4j.Log4j2;
import org.apache.poi.ss.formula.functions.T;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.context.refresh.ContextRefresher;
import org.springframework.core.env.Environment;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.io.IOException;
import java.util.List;

/**
 * @ClassName: MainController
 * @Description:
 * @version 1.0
 * @author: duanhm
 * @Date: 2024/07/24 09:41:15
 * 
 */
@RestController
@Log4j2
@RequiredArgsConstructor
@RequestMapping("/main")
public class MainController {
	private final SysDictService sysDictService;
	@Value("${sensitive.enable}")
	private String sensitiveEnable;

	@Autowired
	private Environment environment;

	@Autowired
	private ContextRefresher contextRefresher;



	/**
	 * 动态设置为非脱敏
	 * 
	 * @return
	 * @throws IOException
	 */
	/*
	 * @GetMapping("/getDynamic") public String getDynamicSensitiveEnable() throws
	 * IOException { // YmlUtil ymlUtil = new YmlUtil();
	 * YmlUtil.updateYaml("application-dev.yml", "sensitive", false); //
	 * ymlUtil.updateYamlFile(); // String dynamicMail =
	 * DynamicMailConfig.getInstance().getDynamicMail();
	 * log.info("sensitiveEnable: {}", sensitiveEnable); // 刷新配置文件
	 * contextRefresher.refresh(); // 使用刷新后的配置 String newstr =
	 * environment.getProperty("sensitive.enable"); log.info("sensitiveEnable: {}",
	 * newstr); return "设置成功,sensitiveEnable===" + newstr; }
	 */

	/**
	 * 
	 * 
	 * @Description:动态设置为非脱敏 开启http://localhost:9110/getDynamic/true
	 * @Description:动态设置为非脱敏 关闭http://localhost:9110/getDynamic/false
	 * @return
	 * @throws IOException
	 */
	@GetMapping("/getDynamic/{sensitiveEnable}")
	public ResultUtil<T> getDynamicSensitiveEnableTrue(@PathVariable("sensitiveEnable") Boolean sensitiveEnable)
			throws IOException {
		YmlUtil.updateYaml("application-dev.yml", "sensitive.enable", sensitiveEnable);
		// ymlUtil.updateYamlFile();
		// String dynamicMail = DynamicMailConfig.getInstance().getDynamicMail();
		log.info("sensitiveEnable: {}", sensitiveEnable);
		// 刷新配置文件
		contextRefresher.refresh();
		// 使用刷新后的配置
		String newstr = environment.getProperty("sensitive.enable");
		log.info("sensitiveEnable: {}", newstr);
		//修改字典数据
		SysDictVo vo=new SysDictVo();
		vo.setSysDictTypeCode("desensitization");
		List<SysDictVo> dictJsonList = sysDictService.getDictJsonList(vo);
		if(dictJsonList.size()>0){
			for (SysDictVo sysDictVo : dictJsonList) {
				if(sensitiveEnable){
					sysDictVo.setCode("1");
				}else{
				    sysDictVo.setCode("0");
				}
				SysDict sysDict=new SysDict();
				BeanUtils.copyProperties(sysDictVo, sysDict);
				sysDictService.updateById(sysDict);
			}

		}
		return ResultUtil.ok();
	}

	@GetMapping("/getEmailDynamic")
	public String getEmailDynamic() throws IOException {
		YmlUtil.updateYaml("application-dev.yml", "email", "dddd@qq.com");
		// ymlUtil.updateYamlFile();
		// String dynamicMail = DynamicMailConfig.getInstance().getDynamicMail();
		// log.info("email: {}", email);
		// 刷新配置文件
		contextRefresher.refresh();
		// 使用刷新后的配置
		String newstr = environment.getProperty("email");
		log.info("email: {}", newstr);
		return "设置成功,email==" + newstr;
	}

}
